package com.ts.proj.controller;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.proj.entity.*;
import com.ts.proj.json.SystemConstantJson;
import com.ts.proj.service.*;
import com.ts.util.JsonUtils;
import com.ts.util.JurisdictionUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import sun.security.krb5.internal.PAData;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 1、决策合同管理
 * 2、投后合同、财务、会议等管理页面跳转
 *
 * */
@Controller
public class ProjectTouHouController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectMeetingService projectMeetingService;

    @Autowired
    private FinanceService financeService;

    @Autowired
    private InformationService informationService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private SystemConstantService systemConstantService;

    @Autowired
    private SystemConstantJson systemConstantJson;

    @Autowired
    private JijinItemService jijinItemService;

    @Autowired
    private PeopleInfoService peopleInfoService;

    @Autowired
    private MarketService marketService;


    /**
     *
     * 决策阶段合同页面跳转
     */

    @RequestMapping("/project-juece-contract-manage/{id}")
    public ModelAndView proJueceMeetManage(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
        if(JurisdictionUtil.isValid()){
            ProjectEntity project = projectService.findById(id);
            modelAndView.addObject("project", project);
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("contractTitle", "决策阶段合同");
                pd.put("proID", project.getId());
                pd.put("userId", systemUserEntity.getId());
                List<ContractEntity> all = contractService.findAll(pd);
                if(all != null){
                    modelAndView.addObject("count",all.size());
                }
            }
            modelAndView.addObject("id", id);
            viewName = "business/juece/project-juece-contract-manage";
            modelAndView.setViewName(viewName);
              return modelAndView;
    }

    @RequestMapping("/project-tuichu-contract-manage/{id}")
    public ModelAndView proTuichuContrantManage(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
        if(JurisdictionUtil.isValid()){
            ProjectEntity project = projectService.findById(id);
            modelAndView.addObject("project", project);
            PageData pd = new PageData();
            pd.put("deleteFlag", "N");
            pd.put("proID", project.getId());
            pd.put("userId", systemUserEntity.getId());
            List<ContractEntity> all = contractService.findAll(pd);
            if(all != null){
                modelAndView.addObject("count",all.size());
            }
        }
        modelAndView.addObject("id", id);
        viewName = "business/tuichu/project-tuichu-contract-manage";
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-touhou-{router}-edit/{id}")
    public ModelAndView projectStateEdit(@PathVariable("id") Integer id,@PathVariable("router") String router){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
        if(JurisdictionUtil.isValid()){
            ProjectEntity project = projectService.findById(id);
            modelAndView.addObject("project", project);
            //财务信息整理
            if(null!=project.getProPaymentMoney()){
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("assignTime", project.getProPaymentTime());
                pd.put("proID", project.getId());
              //  pd.put("userId", systemUserEntity.getId());
                List<FinanceEntity> all = financeService.findAll(pd);
                FinanceEntity financeEntity = null;
                if(all.size()==0){
                    financeEntity = new FinanceEntity();
                }else{
                    financeEntity = all.get(0);
                }
                financeEntity.setUserId(systemUserEntity.getId());
                financeEntity.setDeleteFlag("N");
                financeEntity.setMoneyType("1");
                financeEntity.setYesOrNofinanceReport("1");
                financeEntity.setProID(project.getId());
                financeEntity.setIncomeMoney(String.valueOf(project.getProPaymentMoney()));
                financeEntity.setAssignTime(project.getProPaymentTime());
                financeEntity.setBelongTo(systemUserEntity.getBelongTo());
                financeService.save(financeEntity);
            }

            //会议信息
            if(null!=project.getProLxVotingMeet()){
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("meetName", "立项投决会");
                pd.put("proId", project.getId());
              //  pd.put("userId", systemUserEntity.getId());
                List<ProjectMeetingEntity> all = projectMeetingService.findAll(pd);
                ProjectMeetingEntity projectMeetingEntity = null;
                if(all.size()==0){
                    projectMeetingEntity = new ProjectMeetingEntity();
                }else{
                    projectMeetingEntity = all.get(0);
                }
                projectMeetingEntity.setUserId(systemUserEntity.getId());
                projectMeetingEntity.setDeleteFlag("N");

                projectMeetingEntity.setProId(project.getId());
                projectMeetingEntity.setMeetBrief(project.getProLxVotingMeet());
                projectMeetingEntity.setMeetName("立项投决会");
                projectMeetingEntity.setBelongTo(systemUserEntity.getBelongTo());
                projectMeetingService.save(projectMeetingEntity);
            }
            if(null!=project.getProJcVotintMeet()){
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("meetName", "决策投决会");
                pd.put("proId", project.getId());
             //   pd.put("userId", systemUserEntity.getId());
                List<ProjectMeetingEntity> all = projectMeetingService.findAll(pd);
                ProjectMeetingEntity projectMeetingEntity = null;
                if(all.size()==0){
                    projectMeetingEntity = new ProjectMeetingEntity();
                }else{
                    projectMeetingEntity = all.get(0);
                }
                projectMeetingEntity.setUserId(systemUserEntity.getId());
                projectMeetingEntity.setDeleteFlag("N");

                projectMeetingEntity.setProId(project.getId());
                projectMeetingEntity.setMeetBrief(project.getProJcVotintMeet());
                projectMeetingEntity.setMeetName("决策投决会");
                projectMeetingEntity.setBelongTo(systemUserEntity.getBelongTo());
                projectMeetingService.save(projectMeetingEntity);
            }
            if(null!=project.getProJcDongShiHui()){
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("meetName", "决策董事会");
                pd.put("proId", project.getId());
              //  pd.put("userId", systemUserEntity.getId());
                List<ProjectMeetingEntity> all = projectMeetingService.findAll(pd);
                ProjectMeetingEntity projectMeetingEntity = null;
                if(all.size()==0){
                    projectMeetingEntity = new ProjectMeetingEntity();
                }else{
                    projectMeetingEntity = all.get(0);
                }
                projectMeetingEntity.setUserId(systemUserEntity.getId());
                projectMeetingEntity.setDeleteFlag("N");

                projectMeetingEntity.setProId(project.getId());
                projectMeetingEntity.setMeetBrief(project.getProJcDongShiHui());
                projectMeetingEntity.setMeetName("决策董事会");
                projectMeetingEntity.setBelongTo(systemUserEntity.getBelongTo());
                projectMeetingService.save(projectMeetingEntity);
            }
            if(null!=project.getProJcJiTuan()){
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("meetName", "决策集团董事会");
                pd.put("proId", project.getId());
             //   pd.put("userId", systemUserEntity.getId());
                List<ProjectMeetingEntity> all = projectMeetingService.findAll(pd);
                ProjectMeetingEntity projectMeetingEntity = null;
                if(all.size()==0){
                    projectMeetingEntity = new ProjectMeetingEntity();
                }else{
                    projectMeetingEntity = all.get(0);
                }
                projectMeetingEntity.setUserId(systemUserEntity.getId());
                projectMeetingEntity.setDeleteFlag("N");

                projectMeetingEntity.setProId(project.getId());
                projectMeetingEntity.setMeetBrief(project.getProJcJiTuan());
                projectMeetingEntity.setMeetName("决策集团董事会");
                projectMeetingEntity.setBelongTo(systemUserEntity.getBelongTo());
                projectMeetingService.save(projectMeetingEntity);
            }
            if(null!=project.getProContent()){
                PageData pd = new PageData();
                pd.put("deleteFlag", "N");
                pd.put("contractTitle", "投资阶段合同");
                pd.put("proID", project.getId());
            //    pd.put("userId", systemUserEntity.getId());
                List<ContractEntity> all = contractService.findAll(pd);
                ContractEntity contractEntity = null;
                if(all.size()==0){
                    contractEntity = new ContractEntity();
                }else{
                    contractEntity = all.get(0);
                }
                contractEntity.setUserId(systemUserEntity.getId());
                contractEntity.setDeleteFlag("N");
                contractEntity.setProID(project.getId());
                contractEntity.setContractTitle("投资阶段合同");
                contractEntity.setContractContent(project.getProContent());
                contractEntity.setBelongTo(systemUserEntity.getBelongTo());
                contractService.save(contractEntity);
            }
            //基金item
            if(("jijin").equals(router)){
                PageData pageData = new PageData();
                pageData.put("deleteFlag", "N");
                pageData.put("proID",project.getId());
                List<JijinItemEntity> jijinItemEntityList = jijinItemService.findAll(pageData);
                if(jijinItemEntityList != null && jijinItemEntityList.size()>0) {
                    modelAndView.addObject("jijinItemNumber", jijinItemEntityList.size());
                }else {
                    modelAndView.addObject("jijinItemNumber", 0);
                }

            }
            PageData pd = new PageData();
            pd.put("deleteFlag", "N");
            pd.put("proId", id);
            pd.put("proID", id);
            long meetCount = projectMeetingService.count(pd);
            long financeCount = financeService.count(pd);
            long infoCount = informationService.count(pd);
            long marketCount = marketService.count(pd);
            long contractCount = contractService.count(pd);
            long peopleInfoCount = peopleInfoService.count(pd);
            modelAndView.addObject("meetCount", meetCount);
            modelAndView.addObject("financeCount", financeCount);
            modelAndView.addObject("infoCount", infoCount);
            modelAndView.addObject("marketCount", marketCount);
            modelAndView.addObject("contractCount", contractCount);
            modelAndView.addObject("peopleInfoCount", peopleInfoCount);

            modelAndView.addObject("id", id);
            viewName = "business/project/project-touhou-"+router+"-edit";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-meet/{id}")
    public ModelAndView projectMeeting(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            viewName = "business/touhou/meeting";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-info/{id}")
    public ModelAndView projectInfo(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){

            ProjectEntity byId = projectService.findById(id);
            modelAndView.addObject("byId", byId);
            viewName = "business/touhou/info-list";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
    /**
     * 投后合同管理页面跳转
     *
     * @param id（项目id）
     *
     * */
    @RequestMapping("/project-contract/{id}")
    public ModelAndView projectContract(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ProjectEntity byId = projectService.findById(id);
            modelAndView.addObject("byId", byId);
            viewName = "business/touhou/contract-list";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 决策合同详情页面跳转
     *
     * @param id（项目id）
     *
     * */
    @RequestMapping("/project-contract-view/{state}/{id}")
    public ModelAndView projectJueceContract(@PathVariable("id") Integer id,
                                             @PathVariable("state") String state){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ProjectEntity byId = projectService.findById(id);
            modelAndView.addObject("byId", byId);
            if("juece".equals(state)){
                viewName = "business/juece/contract-list";
            }else if("touhou".equals(state)){
                viewName = "business/touhou/contract-list";
            }else if("tuichu".equals(state)){
                viewName = "business/tuichu/contract-list";
            }
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
    @RequestMapping("/project-meet-add/{id}")
    public ModelAndView projectMeetAdd(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("localId", -1);
            modelAndView.addObject("meet", null);
            viewName = "business/touhou/meet-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-finance-add/{id}/{state}")
    public ModelAndView projectFinanceAdd(@PathVariable("id") Integer id,@PathVariable("state")  String state){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("localId", -1);
            modelAndView.addObject("finance", null);
            if("payment-detail".equals(state)){
                viewName = "business/finance/finance-add";
            }else {
                viewName = "business/touhou/finance-add";
            }
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
    @RequestMapping("/project-info-add/{id}")
    public ModelAndView projectInfoAdd(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("localId", -1);
            modelAndView.addObject("info", null);
            viewName = "business/touhou/info-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }




    /**
     * 决策合同添加页面跳转
     * */
    @RequestMapping("/project-contract-add/{state}/{id}")
    public ModelAndView projectJueceContractAdd(@PathVariable("id") Integer id,
                                                @PathVariable("state") String state){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("localId", -1);
            modelAndView.addObject("contract", null);
            List<SystemConstantEntity> pageList = (List<SystemConstantEntity>) systemConstantJson.constantsByType("project_contract_performance");
            if(pageList != null && pageList.size()>0) {
                modelAndView.addObject("contractPerformanceList", pageList);
            }
            if("juece".equals(state)){
                viewName = "business/juece/contract-add";
            }else if("touhou".equals(state)){
                viewName = "business/touhou/contract-add";
            }else if("tuichu".equals(state)){
                viewName = "business/tuichu/contract-add";
            }
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-meet-edit/{id}")
    public ModelAndView projectMeetAddEdit(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ProjectMeetingEntity projectMeetingEntity = projectMeetingService.findById(id);
            modelAndView.addObject("id", projectMeetingEntity.getProId());
            modelAndView.addObject("localId", id);
            modelAndView.addObject("meet", projectMeetingEntity);
            viewName = "business/touhou/meet-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-peopleInfo-addOrEdit/{proId}/{peopleId}")
    public ModelAndView peopleInfoAddEdit(@PathVariable("proId") Integer proId,
                                          @PathVariable("peopleId") Integer peopleId){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            PeopleInfoEntity byId = peopleInfoService.findById(peopleId);

            modelAndView.addObject("proId", proId);
            modelAndView.addObject("peopleId", peopleId);
            modelAndView.addObject("peopleInfo", byId);
            viewName = "business/touhou/peopleInfo-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-finance-edit/{id}/{state}")
    public ModelAndView projectFinanceAddEdit(@PathVariable("id") Integer id,@PathVariable("state") String state){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            FinanceEntity financeEntity = financeService.findById(id);
            modelAndView.addObject("id", financeEntity.getProID());
            modelAndView.addObject("localId", id);
            modelAndView.addObject("finance", financeEntity);
            if("payment-detail".equals(state)){
                viewName = "business/finance/finance-add";
            }else {
                viewName = "business/touhou/finance-add";
            }
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
    @RequestMapping("/project-info-edit/{id}")
    public ModelAndView projectInfoAddEdit(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            InformationEntity informationEntity = informationService.findById(id);
            modelAndView.addObject("id", informationEntity.getProID());
            modelAndView.addObject("localId", id);
            modelAndView.addObject("info", informationEntity);
            viewName = "business/touhou/info-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
    @RequestMapping("/project-contract-edit/{state}/{id}")
    public ModelAndView projectContractAddEdit(@PathVariable("id") Integer id,
                                               @PathVariable("state")String state){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ContractEntity contractEntity = contractService.findById(id);
            modelAndView.addObject("id", contractEntity.getProID());
            modelAndView.addObject("localId", id);
            modelAndView.addObject("contract", contractEntity);
            List<SystemConstantEntity> pageList = (List<SystemConstantEntity>) systemConstantJson.constantsByType("project_contract_performance");
            if(pageList != null && pageList.size()>0) {
                modelAndView.addObject("contractPerformanceList", pageList);
            }
            if("juece".equals(state)){
                viewName = "business/juece/contract-add";
            }else if("touhou".equals(state)){
                viewName = "business/touhou/contract-add";
            }else if("tuichu".equals(state)){
                viewName = "business/tuichu/contract-add";
            }
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }



    @RequestMapping("/project-touhou-meet/{id}")
    public ModelAndView projectMeetEdit(@PathVariable("id") Integer id){
        ModelAndView mv = this.getModelAndView();
        String vN = "login";
        if(JurisdictionUtil.isValid()){
            mv.addObject("id", id);
            vN = "business/touhou/touhou-meet-edit";
        }
        mv.setViewName(vN);
        return mv;
    }

    //id:projecetId
    @RequestMapping("/project-touhou-upload/{id}/{desc}")
    public ModelAndView projectMeetUpload(@PathVariable("id") Integer id,@PathVariable("desc") String desc){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("fileName", desc);
            viewName = "business/touhou/touhou-upload";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }



    @RequestMapping("/project-juece-upload/{id}/{desc}")
    public ModelAndView projectJueceMeetUpload(@PathVariable("id") Integer id,@PathVariable("desc") String desc){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("fileName", desc);
            viewName = "business/juece/juece-upload";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-tuichu-upload/{id}/{desc}")
    public ModelAndView projecttuichuMeetUpload(@PathVariable("id") Integer id,@PathVariable("desc") String desc){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("fileName", desc);
            viewName = "business/tuichu/tuichu-upload";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-finance/{id}")
    public ModelAndView projectFinance(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";

        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            viewName = "business/touhou/finance";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/project-peopleInfo/{id}")
    public ModelAndView peopleInfo(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";

        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            viewName = "business/touhou/peopleInfo-list";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 双击会议纪要详情页面
     * */
    @RequestMapping("/project-meetDetail/{id}")
    public ModelAndView projectDetail(@PathVariable("id") String id){

        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ProjectMeetingEntity byId = projectMeetingService.findById(Integer.valueOf(id));
            modelAndView.addObject("meet",byId);
            viewName = "business/touhou/meet-detail";
        }
        modelAndView.setViewName(viewName);

        return modelAndView;

    }

    /**
     * 双击显示合同详情
     * */
    @RequestMapping("/project-contractDetail/{id}")
    public ModelAndView projectContractDetail(@PathVariable("id") String id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ContractEntity byId = contractService.findById(Integer.valueOf(id));
            PageData pageData = new PageData();
            pageData.put("deleteFlag","N");
            pageData.put("proID", byId.getProID());
            String proName = projectService.findAll(pageData).get(0).getProName();
            modelAndView.addObject("contract",byId);
            modelAndView.addObject("proName",proName);
            viewName = "business/touhou/contract";
        }
        modelAndView.setViewName(viewName);

        return modelAndView;

    }
}
